package cn.com.connected.common.emnus;

import cn.com.connected.base.exception.BusinessException;
import cn.com.connected.base.exception.ErrorCode;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public enum FileType {

	WX_HEAD_IMG(1,"头像","connected/car/set/headimg","jpg",1),

	DYNAMIC_IMG(2,"动态图片","connected/car/dynamic/img","jpg",1),
	DYNAMIC_VIDEO(3,"动态视频","connected/car/dynamic/video","mp4",1),
	DYNAMIC_AUDIO(4,"动态语音","connected/car/dynamic/audio","mp3",1),

	DYNAMIC_COMMENT_IMG(5,"动态评论图片","connected/car/dynamic/img","jpg",1),
	DYNAMIC_COMMENT_VIDEO(6,"动态评论视频","connected/car/dynamic/video","mp4",1),
	DYNAMIC_COMMENT_AUDIO(7,"动态评论语音","connected/car/dynamic/audio","mp3",1),

	;

	private Integer value;
	private String name;
	private String folder;

	private String defaultPostfix;
	//0:需授权 1:公开
	private int permission;

	FileType(Integer value, String name, String folder,String defaultPostfix,int permission){
		this.value = value;
		this.name = name;
		this.folder = folder;
		this.defaultPostfix = defaultPostfix;
		this.permission = permission;
	}
	public Integer getValue(){
		return value;
	}
	public String getName(){
		return name;
	}
	public String getFolder(){
		return folder;
	}
	public String getDefaultPostfix() {
		return defaultPostfix;
	}
	public int getPermission() {
		return permission;
	}

	public static List<Map> toMap() {
		return Arrays.stream(FileType.values()).map(o->new HashMap(){{put("name",o.getName());put("value",o.getValue());}}).collect(Collectors.toList());
	}

	public static FileType getByValue(Byte value){
		for (FileType fileType : FileType.values()) {
			if (fileType.getValue().intValue() == value.intValue()){
				return fileType;
			}
		}
		throw new BusinessException(ErrorCode.SERIOUS_REQUEST);
	}

	public static String createOssKey(Integer fileType){

		FileType type = getFileType(fileType);

		String str = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));

		return type.getFolder()+ "/" + str + "/";
	}

	public static FileType getFileType(Integer fileType){
		if( fileType==null){
			throw new BusinessException(ErrorCode.WARING,"文件类型不存在");
		}

		FileType type = Arrays.stream(FileType.values()).filter(o->o.getValue().equals(fileType)).findAny().orElse(null);

		if(type == null){
			throw new BusinessException(ErrorCode.WARING,"文件类型不存在");
		}
		return type;
	}

}
